package com.itheima.project_cloth.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.project_cloth.mapper.ClothMapper;
import com.itheima.project_cloth.mapper.OrderMerchantMapper;
import com.itheima.project_cloth.pojo.Cloth;
import com.itheima.project_cloth.pojo.OrderMerchant;
import com.itheima.project_cloth.pojo.PageBean;
import com.itheima.project_cloth.service.OrderMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderMerchantServiceImpl implements OrderMerchantService {
    @Autowired
    private OrderMerchantMapper orderMerchantMapper;
    @Autowired
    private ClothMapper clothMapper;

    @Override
    public OrderMerchant selectOrderMerchantByName(String name) {
        return orderMerchantMapper.selectOrderMerchantByName(name);
    }

    @Override
    public List<OrderMerchant> selectOrderMerchantByCloth(String name) {
        Cloth cloth = clothMapper.selectClothByName(name);

        List<OrderMerchant> list = orderMerchantMapper.selectOrderMerchantByCloth(cloth.getId());

        return list;
    }

    @Override
    public void insertOrderMerchan(OrderMerchant orderMerchant) {
        orderMerchant.setBeginTime(LocalDateTime.now());
        LocalDateTime finish = LocalDateTime.now().plusMinutes(30);
        orderMerchant.setFinishTime(finish);


        orderMerchantMapper.insertOrderMerchan(orderMerchant);
    }

    @Override
    public PageBean selectByTime(Integer page, Integer maxPage, LocalDateTime beginTime, LocalDateTime endTime) {
        PageHelper.startPage(page,maxPage);
        List<OrderMerchant> orderMerchantList =orderMerchantMapper.selectByTime(beginTime,endTime);
        Page<OrderMerchant> p = (Page<OrderMerchant>) orderMerchantList;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }
}
